package com.nanjia.boot.product.spec.value.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nanjia.boot.common.result.PageResult;
import com.nanjia.boot.common.result.Result;
import com.nanjia.boot.common.util.IPUtils;
import com.nanjia.boot.product.spec.group.model.entity.SpecGroup;
import com.nanjia.boot.product.spec.group.model.query.SpecGroupQuery;
import com.nanjia.boot.product.spec.group.service.SpecGroupService;
import com.nanjia.boot.product.spec.value.model.form.SpecValueForm;
import com.nanjia.boot.product.spec.value.model.query.SpecValueQuery;
import com.nanjia.boot.product.spec.value.model.vo.SpecValueVO;
import com.nanjia.boot.product.spec.value.service.SpecValueService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 规格值前端控制层
 *
 * @author 周洋
 * @since 2025-07-09 17:16
 */
@Tag(name = "规格值接口")
@RestController
@RequestMapping("/api/v1/spec-value")
@RequiredArgsConstructor
public class SpecValueController  {

    private final SpecValueService specValueService;

    private final SpecGroupService specGroupService;

    @Operation(summary = "规格值分页列表")
    @GetMapping("/page")
   //@PreAuthorize(@ss.hasPerm('spec:value:query')")
    public PageResult<SpecValueVO> getSpecValuePage(SpecValueQuery queryParams ) {
        IPage<SpecValueVO> result = specValueService.getSpecValuePage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增规格值")
    @PostMapping
   //@PreAuthorize(@ss.hasPerm('spec:value:add')")
    public Result<Void> saveSpecValue(@RequestBody @Valid SpecValueForm formData , HttpServletRequest request) {
        boolean result = specValueService.saveSpecValue(formData, IPUtils.getIpAddr(request));
        return Result.judge(result);
    }

    @Operation(summary = "获取规格值表单数据")
    @GetMapping("/{id}/form")
   //@PreAuthorize(@ss.hasPerm('spec:value:edit')")
    public Result<SpecValueForm> getSpecValueForm(
        @Parameter(description = "规格值ID") @PathVariable Long id
    ) {
        SpecValueForm formData = specValueService.getSpecValueFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改规格值")
    @PutMapping(value = "/{id}")
   //@PreAuthorize(@ss.hasPerm('spec:value:edit')")
    public Result<Void> updateSpecValue(
            @Parameter(description = "规格值ID") @PathVariable Long id,
            @RequestBody @Validated SpecValueForm formData, HttpServletRequest request
    ) {
        boolean result = specValueService.updateSpecValue(id, formData,IPUtils.getIpAddr(request));
        return Result.judge(result);
    }

    @Operation(summary = "删除规格值")
    @DeleteMapping("/{ids}")
   //@PreAuthorize(@ss.hasPerm('spec:value:delete')")
    public Result<Void> deleteSpecValues(
        @Parameter(description = "规格值ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = specValueService.deleteSpecValues(ids);
        return Result.judge(result);
    }

    @Operation(summary = "规格值分页列表")
    @GetMapping("/getSpecGroupList")
   //@PreAuthorize(@ss.hasPerm('spec:value:query')")
    public Result<List<SpecGroup>> getSpecGroupList(SpecGroupQuery queryParams ) {
        List<SpecGroup> result = specGroupService.getSpecGroupList(queryParams);
        return Result.success(result);
    }
}
